package com.polaris.live.adapter.gift

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.databinding.AdapterGiftRechargeBannerBinding
import com.polaris.live.resp.back_resp.GiftBannerVo
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

/**
 * 礼物banner
 *
 * @author Created by 半仙 on 2024/7/29/029 20:56
 */
class GiftBannerAdapter : BaseBannerAdapter<GiftBannerVo>() {

    override fun createViewHolder(parent: ViewGroup, itemView: View, viewType: Int): BaseViewHolder<GiftBannerVo> {
        return GiftBannerViewHolder(AdapterGiftRechargeBannerBinding.bind(itemView))
    }

    override fun bindData(holder: BaseViewHolder<GiftBannerVo>, data: GiftBannerVo?, position: Int, pageSize: Int) {
        if (holder is GiftBannerViewHolder) {
            holder.binding.apply {
                data?.let {
                    cosType1.visibility = View.GONE
                    cosType2.visibility = View.GONE
                    Glide.with(root.context).load(ImageUtils.imgPath(data.banner)).into(ivBack)
                    when (data.type) {
                        CommonConst.GiftBanner.ACT, CommonConst.GiftBanner.PRIVILEGE -> {
                            cosType1.visibility = View.VISIBLE
                            Glide.with(root.context).load(ImageUtils.imgPath(data.giftPic)).into(ivActIcon)
                            tvActDetail.text = data.content
                        }

                        CommonConst.GiftBanner.NAMING -> {
                            cosType2.visibility = View.VISIBLE
                            avNaming.setImageUrl(data.avatar, data.userId)
                            avFvNaming.setFrameImage(FrameImageUtils.urlToFrameImageBean(data.avatarFrame))
                            Glide.with(root.context).load(ImageUtils.imgPath(data.giftPic)).into(ivNamingIcon)
                            tvNamingDetail.text = createSpan(tvNamingDetail, data.content).create()
                        }
                    }
                }
            }
        }
    }

    //定义文字图片拼接
    fun createSpan(textView: TextView, content: String): SpanUtils {
        val span = SpanUtils.with(textView)
        val splitMessage = content.split(CommonConst.TextImageSpan.SPLIT)
        splitMessage.forEach {
            if (it.contains(CommonConst.TextImageSpan.TEXT)) {
                val modifiedStr = it.replace(CommonConst.TextImageSpan.TEXT, "")
                span.append(modifiedStr)
                    .setForegroundColor(ColorUtils.getColor(R.color.color_fff200))
            } else {
                span.append(it)
            }
        }
        return span
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.adapter_gift_recharge_banner
    }

    class GiftBannerViewHolder(val binding: AdapterGiftRechargeBannerBinding) :
        BaseViewHolder<GiftBannerVo>(binding.root)

}